<?php
namespace frontend\controllers;
use yii;

/**
 * Post controller
 */
class GithookController extends FController
{

    public $enableCsrfValidation = false;  //必须加上此选项

    /**
     * osgit 钩子
     * @return [type] [description]
     */
    public function actionOs(){

        // $data=json_decode(file_get_contents("php://input"),true);
        // if(empty($data))
        //  return false;
        $data = file_get_contents("php://input");
        //yii::log(var_export($data,true),'warning','osgit');
        $data = str_replace('hook=', '', urldecode($data));
        $data = json_decode($data, 1);
        //yii::log(var_export($data,true),'warning','osgit');
        if (empty($data)) {
            return false;
        }
        $ref = $data['push_data']['ref'];
        //yii::log($ref,'warning','osgit');
        if (preg_match('#dev#', $ref)) {
            $branch = 'dev';
        } elseif (preg_match('#master#', $ref)) {
            $branch = 'master';
        } else {
            //yii::log('no such branch','warning','osgit');
        }
        $projectName = $data['push_data']['repository']['name'];
        $gitcafe_value = $data['push_data']['commits']['0']['id'];

        if (!$projectName || !$gitcafe_value) {
            //yii::log('no projectName or gitcafe_value','warning','osgit');
        }

        $create_time = date('Y-m-d H:i:s');
        $sql = "update deploy set gitcafe_value='$gitcafe_value', create_time='$create_time' where name='{$projectName}' and branch='{$branch}'  ";
        
        $flag = Yii::$app->db->createCommand($sql)->execute();


        return 1;
    }

}